import { createRouter, createWebHistory,createWebHashHistory} from "vue-router";
import Cookies from "js-cookie";
import index from "../views/Index.vue";



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      redirect:'/index/home',
    },
    {
      path: "/index",
      name: "index",
      component: index,
       // 刷新页面
     children:[
      {
        path:'redirect/:backpath(.*)',
        name:'redirect',
       component:()=>import("@/views/redirect/index.vue"),
       meta:{
          title:''
       }
      },
      {
        path:'admin',
        name:'admin',
        component:()=>import("@/views/admin/Personaldata.vue"),
        meta:{
          title:'admin.personinfo'
        }
      },
      {
        path:'chengpassword',
        name:'chengpassword',
        component:()=>import("@/views/admin/ChangePassword.vue"),
        meta:{
          title:'admin.revamppwd'
        }
      }
    ]
    },
    {
      path:'/Login',
      name:"LoginView",
      component:()=>import("@/views/Login.vue")
    },
    {
      path:'/404',
      name:"404",
      component:()=>import("@/views/404.vue")
    }
  ],
});



router.beforeEach((to,from)=>{
  const token = Cookies.get("token")
  if(!token && to.name!="LoginView"){
    // 回到登录页面
    router.replace({path:'/Login',query:{back:to.fullPath}});
    return false
  }
  return true
})



export default router;
